package com.lykj.llw.box.adapter

import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lykj.llw.box.ui.activity.ImageDisplayActivity
import com.lykj.lib_base.component.GlideApp
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.R
import com.lykj.llw.box.data.model.PicModel
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import me.jessyan.autosize.utils.AutoSizeUtils

/**
 * ================================================
 *
 * ================================================
 *
 * @author gosha
 * @date 2023/1/17-14:21
 */
class PicAdapter : BaseMultiItemQuickAdapter<PicModel, BaseViewHolder>() {
    var canDelete = false

    init {
        addItemType(1, R.layout.item_add)
        addItemType(0, R.layout.item_pic)
    }

    override fun convert(holder: BaseViewHolder, item: PicModel) {
        when (holder.itemViewType) {
            1 -> {
                LogUtils.e("使用有添加的布局")
            }

            0 -> {
                val m = MultiTransformation(
                    CenterCrop(),
                    RoundedCorners(
                        AutoSizeUtils.dp2px(
                            context, 7.5f
                        )
                    ),
                )
                GlideApp.with(context)
                    .load(item.remoteUrl)
                    .apply(
                        RequestOptions.bitmapTransform(m)
                    )
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(holder.getView(R.id.iv_pic))

                if (canDelete) {
                    holder.getView<ImageView>(R.id.iv_delete).visibility = View.VISIBLE
                } else {
                    holder.getView<ImageView>(R.id.iv_delete).visibility = View.GONE
                }
                holder.itemView.clickWithTrigger {
                    val intent = Intent(context, ImageDisplayActivity::class.java)
                    intent.putExtra("pic", item.localUrl)
                    context.startActivity(intent)
                }
            }
        }
    }

    open fun canDelete(canDelete: Boolean) {
        this.canDelete = canDelete
    }
}